<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<style type="text/css">
    .tdcc{
        height:40px;
        width: 80px;
        background-color: rgb(250,250,250);
    }
</style>
<!--添加或修改企业弹窗-->
<div class="easyui-dialog" id="editCompany_dialog" title="添加企业" data-options="closed:true,modal:true,width:800,height:340">
            <form id="editForm" method="post">
                <input id="companyId" name="id" hidden>
                <table class="grid" align="center" style="margin-top: 20px" >
                    <tr style="border: 10px">
                        <td class="tdcc"><lable for="name">企业名称:</lable></td>
                        <td><input class="easyui-validatebox" name="name" data-options="required:true" /></td>
                        <td class="tdcc"><lable for="linkman">联系人:</lable></td>
                        <td><input class="easyui-validatebox" name="linkman" data-options="required:true" /></td>
                        <td class="tdcc" rowspan="4">logo预览</td>
                        <td rowspan="4"><img id="cmpanyimg" src="" ></td>
                    </tr>
                    <tr >
                        <td  class="tdcc"><lable for="type">类型:</lable></td>
                        <td><!--1私企，2国营，3事业-->
                            <select id="type_id" class="easyui-combobox" data-options="panelHeight:'auto',required:true" name="type"  style="width: 150px"/>
                            <option value="1">私企</option>
                            <option value="2">国企</option>
                            <option value="3">事业</option>
                            </select>
                        </td>
                        <td class="tdcc"><lable for="url">联系人性别:</lable></td>
                        <td>
                            <span></span><input  id="provider0" name="sex" type="radio" value="0" />男&nbsp;&nbsp;&nbsp;&nbsp;
                            <span></span><input  id="provider1" name="sex" type="radio" value="1" />女
                        </td>
                    </tr>
                    <tr >
                        <td class="tdcc"><lable for="telephone">座机:</lable></td>
                        <td><input class="easyui-validatebox" name="telephone" data-options="required:true"/></td>
                        <td class="tdcc"><lable for="phone">手机:</lable></td>
                        <td><input class="easyui-validatebox" name="phone" data-options="required:true"/></td>
                    </tr>
                    <tr >
                        <td class="tdcc"><lable for="url">网址:</lable></td>
                        <td><input class="easyui-validatebox" name="url" type="url" data-options="required:true"/></td>
                        <td class="tdcc"><lable for="email">邮箱</lable></td>
                        <td ><input class="easyui-validatebox" name="email" type="email" data-options="required:true"/></td>
                    </tr>
                    <tr >
                        <td class="tdcc"><lable for="userId">关联用户:</lable></td>
                        <td>
                            <input type="text" name="userId" id="userId"  hidden/>
                            <input type="text" id="autocomplete-ajax" data-options="required:true" style="position: absolute; z-index: 2; background: transparent;"/>
                            <input type="text" id="autocomplete-ajax-x" disabled="disabled" style="color: #CCC; position: absolute; background: transparent; z-index: 1;"/>
                        </td>
                        <td class="tdcc"><lable for="address">详细地址:</lable></td>
                        <td><input class="easyui-validatebox" name="address" data-options="required:true"/></td>
                        <td class="tdcc"><lable for="logo">logo:</lable></td>
                        <td>
                            <div id="upload_ue"></div>
                            <input class="easyui-validatebox"  id="logo"  readonly="readonly"  name="logo" data-options="required:true" />
                            <a href="javascript:void(0);"  class="easyui-linkbutton" data-options="iconCls:'icon-edit'" onclick="upImage();">上传图片</a></td>
                    </tr>
                    <tr >
                        <td class="tdcc"><lable for="platform">省:</lable></td>
                        <td>
                            <input id="delivery_address_province" class="easyui-combobox"  name="provinc"  value=""
                                   data-options="missingMessage:'请选择省份',
                                         url:'${path}/areaInfo/addressList.do?type=1',
                                         valueField:'id',
                                         textField:'name',
                                         required:true,
                                         editable:false,
                                         onLoadSuccess:function(data){
                                            if (data.length > 0) {
                                                if (provinceId != 0) {
                                                    $(this).combobox('setValue', provinceId);
                                                } else {
                                                    $(this).combobox('setValue',data[0].id);
                                                    $(this).combobox('setText',data[0].name);
                                                }
                                            }
                                         },
                                         onChange: function (newValue, oldValue){
                                             $('#delivery_address_city').combobox('reload','${path}/areaInfo/addressList.do?type=2&pid='+newValue);
                                         },
                                         onSelect:function(data){
                                             provinceId = 0;
                                             cityId = 0;
                                             areaId = 0;
                                         }" />
                        </td>
                        <td class="tdcc"><lable for="platform">市:</lable></td>
                        <td>
                            <input id="delivery_address_city" class="easyui-combobox "  name="city"  value=""
                                   data-options="missingMessage:'请选择城市',
                                         valueField:'id',
                                         textField:'name',
                                         required:true,
                                         editable:false,
                                         onLoadSuccess:function(data){
                                            if (data.length > 0 ) {
                                                if (cityId != 0) {
                                                    $(this).combobox('setValue',cityId);
                                                } else {
                                                    $(this).combobox('setValue',data[0].id);
                                                    $(this).combobox('setText',data[0].name);
                                                }
                                            } else {
                                                $('#delivery_address_city').combobox('clear');
                                            }
                                         },
                                         onChange: function (newValue, oldValue){
                                             $('#delivery_address_area').combobox('reload','${path}/areaInfo/addressList.do?type=3&pid='+newValue);
                                         },
                                         onSelect:function(data){
                                             cityId = 0;
                                             areaId = 0;
                                         }"/>
                        </td>
                        <td class="tdcc"><lable for="platform">区:</lable></td>
                        <td>
                            <input id="delivery_address_area" class="easyui-combobox "  name="districk"  value=""
                                   data-options="missingMessage:'请选择区县' ,
                                         valueField:'id' ,
                                         textField:'name',
                                         required:true,
                                         editable:false,
                                         onLoadSuccess:function(data){
                                            if (data.length > 0) {
                                                if (areaId != 0) {
                                                    $(this).combobox('setValue',areaId);
                                                } else {
                                                    $(this).combobox('setValue',data[0].id);
                                                    $(this).combobox('setText',data[0].name);
                                                }
                                            } else {
                                                $('#delivery_address_area').combobox('clear');
                                            }
                                         },
                                         onSelect:function(data){
                                             areaId = 0;
                                         }" />
                        </td>
                    </tr>
                </table>
                <div style="margin-top: 20px;text-align: center">
                    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveCompany()">保存</a>
                    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="$('#editCompany_dialog').dialog('close')">取消</a>
                </div>
            </form>
</div>
<!--添加员工-->
<div class="easyui-dialog" id="editEmployee_dialog" title="添加员工" data-options="closed:true,modal:true,width:340,height:320" style="padding: 1em">
    <form id="editempForm" method="post">
        <input name="id" hidden/>
        <input name="companyId" hidden/>
        <input name="deptId" hidden/>
        <table class="grid">
            <tr>
                <td class="tdcc"><lable for="name">姓名:</lable></td>
                <td><input class="easyui-validatebox" name="name" data-options="required:true"/></td>
            </tr>
            <tr>
                <td class="tdcc"><lable for="sex">性别:</lable></td>
                <td> <span></span><input  name="sex" type="radio" value="0" />男&nbsp;&nbsp;&nbsp;&nbsp;
                    <span></span><input  name="sex" type="radio" value="1" />女</td>
            </tr>
            <tr>
                <td class="tdcc"><lable for="phone">电话:</lable></td>
                <td><input class="easyui-validatebox" name="phone"  data-options="required:true"/></td>
            </tr>
            <tr>
                <td class="tdcc"><lable for="card">身份证号:</lable></td>
                <td><input class="easyui-validatebox" name="card" data-options="required:true"/></td>
            </tr>
            <tr>
                <td class="tdcc"><lable for="email">邮箱:</lable></td>
                <td><input class="easyui-validatebox" name="email"  data-options="required:true"/></td>
            </tr>
            <tr>
                <td class="tdcc"><lable for="deptname">部门:</lable></td>
                <td><input  name="deptname" readonly/></td>
            </tr>
        </table>
        <div style="margin-top: 20px;text-align: center">
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="saveEmploy()">保存</a>
            <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="$('#editEmployee_dialog').dialog('close')">取消</a>
        </div>
    </form>
</div>

<div class="easyui-dialog" id="addEmployee_dialog" title="导入员工" data-options="closed:true,modal:true,width:600,height:200" style="padding: 1em" >
    <form id="excelForm" method="post" enctype="multipart/form-data">
        <input name="companyId" hidden>
        <input name="deptId" hidden>
        部门：
        <input name="deptname" >&nbsp;&nbsp;&nbsp;&nbsp;
        Excel文件：<input class="easyui-validatebox" id="excelId"  type="file" name="excelFile" data-options="required:true"/>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="display: inline-block;float: right;" onclick="uploadEmploys()">确定</a>
    </form>
    <div style="text-align: center;display: block;margin-top: 1em;">
        <img src="../../../static/style/images/excelmodel1.png">
    </div>
</div>

<script type="text/javascript">
    $(function() {
        _editor = UE.getEditor('upload_ue',{
            serverUrl:"${path}/file/upload.do?compress=1"
        });
        _editor.ready(function () {
            _editor.hide();
            //侦听图片上传
            _editor.addListener('beforeInsertImage', function (t, arg) {
                //有多少张图片arg.length就是多大
                $("#logo").val(arg[0].src);
                $("#cmpanyimg").attr("src",arg[0].src);
            });
        });
    });
    var rowid;
    var flag = "add";
    var provinceId = 0;
    var cityId = 0;
    var areaId = 0;
    //联动处理
    function rowchange(id) {
        rowid=id;
        var queryParams = {};
        queryParams.companyId = id;
        var options = $("#Employ_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#Employ_datagrid").datagrid('options').queryParams = queryParams;
        $("#Employ_datagrid").datagrid('reload');

        $("#dept_datagrid").datagrid('options').queryParams = queryParams;
        $("#dept_datagrid").datagrid('reload');
    }
    function rowchange2(id) {
        var queryParams = {};
        queryParams.companyId = rowid;
        queryParams.deptId = id;
        var options = $("#Employ_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#Employ_datagrid").datagrid('options').queryParams = queryParams;
        $("#Employ_datagrid").datagrid('reload');
    }
    //筛选企业类型
    function comboxchange(){
        var type = $("#settleType").val();
        var queryParams = {};
        queryParams.type = type;
        //重新加载体检卡明细列表
        var options = $("#company_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#company_datagrid").datagrid('options').queryParams = queryParams;
        $("#company_datagrid").datagrid('reload');
    }
    //搜索企业名
    function searchName(val){
        var queryParams = {};
        queryParams.name = val;
        //重新加载体检卡明细列表
        var options = $("#company_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#company_datagrid").datagrid('options').queryParams = queryParams;
        $("#company_datagrid").datagrid('reload');
    }
    //搜索员工名
    function searchEmploy(val){
        var sex = $("#settleSex").val();
        var queryParams = {};
        queryParams.name = val;
        queryParams.companyId = rowid;
        queryParams.sex = sex;
        //重新加载体检卡明细列表
        var options = $("#Employ_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#Employ_datagrid").datagrid('options').queryParams = queryParams;
        $("#Employ_datagrid").datagrid('reload');
    }
    //重置企业列表
    function refCompany(){
        $("#settleType").val("");
        $("#searchCompany").searchbox('setValue', null);
        var queryParams = {};
        $("#company_datagrid").datagrid('options').queryParams = queryParams;
        $("#company_datagrid").datagrid('reload');
    }
    //打开添加修改弹层
    function openEditBox(str){
        $("#editForm").form("clear");
        flag = str;
        if(str=="add"){
            $("#provider0").attr("checked","checked");
        }else{
            var select = $("#company_datagrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择修改的企业","info");
                return;
            }
            console.log(select);
            $("#editCompany_dialog").dialog({title: "修改企业"});
            $("#editForm").form("load",select);
            provinceId=select.provinc;
            cityId=select.city;
            areaId=select.districk;
            $.get( "/company/getUserName.do?id="+select.userId,
            function(res){
                $("#autocomplete-ajax").val(res);
            });
        }

        //注册自动补全
        $('#autocomplete-ajax').autocomplete({
            serviceUrl: '${path}/company/autoCompleteData.do',
            onSelect: function(suggestion) {
                $('#userId').val(suggestion.data);
            },
            onHint: function (hint) {
                $('#autocomplete-ajax-x').val(hint);
            }
        });
        $("#editCompany_dialog").dialog("open");
    }
    <!--刷新员工列表-->
    function refcardEmployee(){
        $("#settleSex").val("");
        $("#searchEmployname").searchbox('setValue', null);
        var queryParams = {};
        queryParams.companyId=rowid;
        $("#Employ_datagrid").datagrid('options').queryParams = queryParams;
        $("#Employ_datagrid").datagrid('reload');
    }

    <!--打开添加员工弹层-->
    function openUserBox(type){
        flag=type;
        $("#editempForm").form("clear");
        if(type=="add"){
            if(rowid==null){$.messager.alert("提示","请选择企业","info");return;}
            var lastRowIndex=$('#dept_datagrid').datagrid('getRows').length;
            if(lastRowIndex==0){$.messager.alert("提示","请先在右侧添加部门","info");return;}
            var selectdept = $('#dept_datagrid').datagrid("getSelected");
            if(selectdept==null){$.messager.alert("提示","请先在右侧选中部门","info");return;}
            $("[name='companyId']").val(rowid);
            $("[name='deptId']").val(selectdept.id);
            $("[name='deptname']").val(selectdept.name);
        }else{
            var select = $("#Employ_datagrid").datagrid("getSelected");
            if(select == null){
                $.messager.alert("提示","请选择修改的员工","info");
                return;
            }
            console.log(select);
            $("#editEmployee_dialog").dialog({title: "修改员工信息"});
            $("#editempForm").form("load",select);
        }
        $("#editEmployee_dialog").dialog("open");

    }
    //删除企业
    function deleteCompany() {
        var select = $("#company_datagrid").datagrid("getSelected");
        if(select == null){
            $.messager.alert("提示","请选择企业","info");
            return;
        }
        $.messager.confirm("确认","确定要删除吗?\n确定删除将连部门和员工信息一同删除",function(yes){
            if(yes){
                commonDelete("${path}/company/delete.do",select.id,"company_datagrid");
            }
        });
    }
    //删除部门
    function delDept() {
        var select = $("#dept_datagrid").datagrid("getSelected");
        if(select == null){
            $.messager.alert("提示","请选择部门","info");
            return;
        }       $.messager.confirm("确认","确定要删除吗?\n确定删除将连同该部门下员工信息一同删除",function(yes){
            if(yes){
                commonDelete("${path}/company/delDept.do",select.id,"dept_datagrid");
            }
        });
    }

    function edit1 (index,row)
    {
        row.editing = false;
        $('#dept_datagrid').datagrid('refreshRow', index);
    }
    function edit2 (index,row)
    {
        row.editing = true;
        $('#dept_datagrid').datagrid('refreshRow', index);
    }
    //弹出图片上传的对话框
    function upImage() {
        var myImage = _editor.getDialog("insertimage");
        myImage.open();
    }

    //保存企业信息
    function saveCompany(){
        var url = "";
        var str = "";
        if(flag == 'add'){
            url = "${path}/company/add.do";
            str = "添加";
        }else {
            url = "${path}/company/edit.do";
            str = "修改";
        }
        $("#editForm").form("submit",{
            url:url,
            onSubmit:function(){
                var valid = $(this).form("validate");
                if (!valid) {
                    $.messager.progress("close");
                }
                return valid;
            },
            success:function(data){
                $.messager.progress("close");
                data = JSON.parse(data);
                if(data.success){
                    $("#editCompany_dialog").dialog("close");
                    $.messager.show({
                        title:str +"成功",msg:str +"成功!",
                        timeout:600,style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#company_datagrid").datagrid("reload");
                }else{
                    $.messager.alert(str +"失败", data.msg,"error");
                }
            }
        });
    }
    //保存员工信息
    function saveEmploy(){
        var url="/company/addEmployee.do";
        $("#editempForm").form("submit",{
            url:url,
            onSubmit:function(){
                var valid = $(this).form("validate");
                if (!valid) {
                    $.messager.progress("close");
                }
                return valid;
            },
            success:function(data){
                $.messager.progress("close");
                data = JSON.parse(data);
                if(data.success){
                    $("#editEmployee_dialog").dialog("close");
                    $.messager.show({
                        title:"成功",msg: "操作成功!",
                        timeout:600,style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#Employ_datagrid").datagrid("reload");
                }else{
                    $.messager.alert( "操作失败", data.msg,"error");
                }
            }
        })
    }
    //删除员工信息
    function deleteEmp() {
        var selectrow = $("#Employ_datagrid").datagrid("getSelected");
        if(selectrow==null){$.messager.alert( "提示", "请选择要删除的员工","error");}
        commonDelete("${path}/company/delEmployee.do",selectrow.id,"Employ_datagrid");
    }
    //员工性别筛选
    function comboxsex(){
        if(rowid==null){$.messager.alert("提示","请选择企业","info");return;}
        var deptrow = $("#dept_datagrid").datagrid("getSelected");
        var sex = $("#settleSex").val();
        var queryParams = {};
        queryParams.companyId = rowid;
        queryParams.sex = sex;
        if(deptrow!=null){queryParams.deptId =deptrow.id; }
        var options = $("#Employ_datagrid").datagrid('getPager').data("pagination").options;
        options.pageNumber = 1;
        $("#Employ_datagrid").datagrid('options').queryParams = queryParams;
        $("#Employ_datagrid").datagrid('reload');
    }
    //添加行
    function addRow() {
        $('#dept_datagrid').datagrid('appendRow', {});      //追加一个新行。新行将被添加到最后的位置。
        var lastRowIndex=$('#dept_datagrid').datagrid('getRows').length-1;//返回当前页的最后一行。
        $('#dept_datagrid').datagrid('beginEdit', lastRowIndex);    //开始编辑行。
        $('#dept_datagrid').datagrid('selectRow',lastRowIndex); //选中最新行
        $("#editdeptbtn").find(".l-btn-text").text("完成");
    }
    //编辑
    function editDept(){
        if(rowid==null){
            $.messager.alert("提示","请选择企业","info");
            return;
        }
        var type = $("#editdeptbtn").find(".l-btn-text").text();
        var row = $("#dept_datagrid").datagrid("getSelected");
        var index = $('#dept_datagrid').datagrid('getRowIndex', row);
        if(type=="完成"){
            $('#dept_datagrid').datagrid('endEdit', index);    //结束编辑行。
            $.ajax({
                type:"post",dataType:"json",
                url:"/company/addDept.do",
                data:{name:row.name,deptId:row.id,companyId:rowid},
                success:function(){
                    $("#editdeptbtn").find(".l-btn-text").text("编辑");
                    $('#dept_datagrid').datagrid('reload');
                    $('#dept_datagrid').datagrid('selectRow',index);
                }
            });
        }else{
            $('#dept_datagrid').datagrid('beginEdit', index);    //开始编辑行。
            $("#editdeptbtn").find(".l-btn-text").text("完成");
        }
    }
    //打开导入Excel窗口
    function openUploadEmpanyeeBox(){
        if(rowid==null){$.messager.alert("提示","请选择企业","info");return;}
        var lastRowIndex=$('#dept_datagrid').datagrid('getRows').length;
        if(lastRowIndex==0){$.messager.alert("提示","请先添加企业部门","info");return;}
        var selectrow=$('#dept_datagrid').datagrid('getSelected');
        if(selectrow==null){$.messager.alert("提示","请先在右侧选择部门","info");return;}
        $("#excelForm").children("[name='companyId']").val(rowid);
        $("#excelForm").children("[name='deptId']").val(selectrow.id);
        $("#excelForm").children("[name='deptname']").val(selectrow.name);
        $("#addEmployee_dialog").dialog("open");
    }
    //上传Excel
    function uploadEmploys(){
        var file = $("#excelId").val();
            $('#excelForm').form("submit",{
                url:"/company/addEmployeeFromExcel.do",
                onSubmit:function() {
                    progressLoad();
                    var isValid = $(this).form('validate');
                    if (!isValid) {
                        progressClose();
                    }
                    return isValid;
                },
                success:function(result) {
                    progressClose();
                    result = $.parseJSON(result);
                    console.log(result);
                    if (result.success) {
                        $("#addEmployee_dialog").dialog("close");
                        $.messager.show({
                            title:"成功",msg: "导入成功!",
                            timeout:600,style:"left:30%;top:10%",
                            showType:"fade"
                        });
                        $("#Employ_datagrid").datagrid("reload");
                    } else {
                        $.messager.alert("提示",result.msg,"error");
                    }
                }
            });
    }
    //删除公共调用方法
    function commonDelete(url,id,datagrid){
        $.messager.progress();
        $.ajax({
            url:url,
            type:"post",
            data:{id : id},
            dataType:"json",
            success:function(data){
                $.messager.progress("close");
                if(data.success){
                    $.messager.show({
                        title:"删除成功",
                        msg:"删除成功!",
                        timeout:600,
                        style:"left:30%;top:10%",
                        showType:"fade"
                    });
                    $("#"+datagrid).datagrid("reload");
                    $("#"+datagrid).datagrid('clearSelections');
                }else{
                    $.messager.alert("删除失败", data.msg,"error");
                }
            },
            error:function(){
                $.messager.progress("close");
                $.messager.alert("删除失败", "服务器连接失败!","error");
            }
        });
    }
</script>

